<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_News_Services_Auto
{
	public function run()
	{
		ini_set("max_execution_time", 100);
		ini_set('memory_limit', '256M');
		require_once LIB_DIR . DS . 'simplehtmldom' . DS . 'simple_html_dom.php';
		
		$conn = Gio_Db_Connection::getConnection();
		$articleAutoDao = new Modules_News_Models_Mysql_Articleauto();
		$articleAutoDao->setConnection($conn);
		$numArticles = 30;
		$articlesAuto = $articleAutoDao->find($numArticles);

		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		/**
		 * Auto Tags
		 */
		$autoTag = false;
		$allTags = array();
		if ($autoTag) {
//			$tagDao = new Admin_Modules_News_Models_Mysql_Tag();
//			$tagDao->setConnection($conn);
//			$allTags = $tagDao->getAllTags();
		}
		if ($articlesAuto) {
			foreach ($articlesAuto as $index => $articleAuto) {
				$categories = explode('-', $articleAuto['category_ids']);
				
				$article = array(
					'title' => trim($articleAuto['title']),
					'slug' => trim($articleAuto['slug']),
					'created_date' => $articleAuto['created_date'],
					'category_id' => $categories[0],
					'status' => 'active',
					'description' => trim($articleAuto['description']),
					'article_hot' => ($index % 5 == 0) ? 1 : 0,
					'article_sticky' => ($index % 3 != 0) ? 1 : 0,
					'link_source' => $articleAuto['link_source'],
					'image_url' => $articleAuto['image_url'],
					'web_id' => $articleAuto['website'],
					'article_photo' => $articleAuto['article_photo'],
					'article_video' => $articleAuto['article_video'],
					'language'		=> 'vi_VN',
				);
				
				$existsArticle = $articleDao->getByLink($article['link_source']);
				$articleAutoDao->delete($articleAuto['auto_id']);
				
				if (null == $existsArticle) {
					switch ($articleAuto['website']) {
						case 'vtc':
							$article = $this->_vtc($article);
							break;
						case 'vnexpress':
							$article = $this->_vnexpress($article);
							break;
						case 'dantri':
							$article = $this->_dantri($article);
							break;
						case 'thethaovanhoa':
							$article = $this->_thethaovanhoa($article);
							break;
						case 'zing':
							$article = $this->_zing($article);
							break;
						case 'megafun':
							$article = $this->_megafun($article);
							break;
						case 'kenh14':
							$article = $this->_kenh14($article);
							break;
						case 'ngoisao':
							$article = $this->_ngoisao($article);
							break;
						case 'haitugio':
							$article = $this->_haitugio($article);
							break;
						case 'tuoitre':
							$article = $this->_tuoitre($article);
							break;
						case 'thanhnien':
							$article = $this->_thanhnien($article);
							break;
						case 'nguoilaodong':
							$article = $this->_nguoilaodong($article);
							break;
						case 'haisao':
							$article = $this->_haisao($article);
							break;
						case 'autopro':
							$article = $this->_autopro($article);
							break;
						case 'bongda':
							$article = $this->_bongda($article);
							break;
						case 'tintuconline':
							$article = $this->_tintuconline($article);
							break;
					}
					
					if ($article['title'] && $article['description'] && $article['content']) {
						$articleId = $articleDao->add($article);
						if ($articleId) {
							/**
							 * Add to article category assoc
							 */
							$articleDao->addToCategories($articleId, $categories);
						}
					}
				}
			}
		}
	}
	
	private function _vtc($article)
	{
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			
			$html = str_get_html($htmlString);
			
			$pageContent = $html->find('div[id=pageContent]', 0);
			if ($pageContent) {
				$createdDate = $html->find('.date', 0);
				$createdDate = str_replace('/', '-', $createdDate->plaintext);
				$image = $pageContent->find('img');
				
				$content = $pageContent->innertext;
				$content = Gio_Core_String::stripTags($content, array('a'));
				
				$article['content'] = $content;
				$article['image_url'] = (count($image) > 1) ? $image[0]->src : $article['image_url'];
				$article['created_date'] = ($createdDate) 
												? date('Y-m-d H:i:s', strtotime($createdDate)) 
												: $article['created_date'];
			} 
		}
		return $article;
	}
	
	private function _dantri($article)
	{
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			
			require_once LIB_DIR . DS . 'htmlpurifier' . DS . 'HTMLPurifier.auto.php';
			$purifier = new HTMLPurifier();
			//$htmlString = $purifier->purify($htmlString);
			$html = str_get_html($htmlString);

			$pageContent = $html->find('div[id=ctl00_IDContent_ctl00_divContent]', 0);
			
			if ($pageContent) {
				$dateTimeContainer = $pageContent->find('span.mr2', 0);
				if (!isset($dateTimeContainer->innertext) || !$dateTimeContainer->innertext) {
					return $article;
				}

				$dateTime = $dateTimeContainer->innertext;
				$dateTime = explode(',', $dateTime);
				$dateTime = str_replace('-', '', $dateTime[1]);
				$dateTime = explode('/', $dateTime);
				$dateTime = trim($dateTime[1]) . '/' . trim($dateTime[0]) . '/' . trim($dateTime[2]);

				$article['created_date'] = date('Y-m-d H:i:s', strtotime($dateTime));

				$contentContainer = $pageContent->find('div.fon43', 0);
				
				$content = $contentContainer->outertext;
				
				//$content = '<p><b>' . $article['description'] . '</b></p>' . Gio_Core_String::stripTags($content, array('a'));
				$content = '<p><b>' . $article['description'] . '</b></p>' . $content; 
				
				$image = $contentContainer->find('img', 0);
				
				$article['content'] = $content;
				$article['image_url'] = (isset($image->src)) ? $image->src : $article['image_url'];
			} 
		}
		return $article;
	}
	
	private function _thethaovanhoa($article)
	{
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			$html = str_get_html($htmlString);
			$pageContent = $html->find('div[id=ctl00_ContentPlaceHolder1_divDetailNews]', 0);
			if ($pageContent) {
				$contentContainer = $pageContent->find('div[id=divDetail]', 0);
				$content = $contentContainer->innertext; 
				$content = Gio_Core_String::stripTags($content, array('a'));
				
				$image = $contentContainer->find('img', 0);
				$article['content'] = $content;
				$article['image_url'] = (isset($image->src)) ? $image->src : $article['image_url'];
			} 
		}
		return $article;
	}
	
	private function _vnexpress($article)
	{
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");
		$website = 'http://vnexpress.net';
		
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			$html = str_get_html($htmlString);
			$pageContent = $html->find('div.content', 0);
			$container = ($pageContent) ? $pageContent->find('div', 0) : null;
			if ($container) {
				$title = $container->find('.Title', 0)->outertext;
				$content = str_replace($title, '', $container->innertext);
				
				$aArray = $container->find('a');
				if (count($aArray) > 0) {
					for ($i = 0 ; $i < count($aArray); $i++) {
						$href = strtolower($aArray[$i]->href);
						if (strpos($href, 'page_') !== false) {
							$tempLink = $website . $aArray[$i]->href;
							$tempContent = @file_get_contents($tempLink);
							$tempHtml = str_get_html($tempContent);
							$tempPageContent = $tempHtml->find('div.content', 0);
							$tempContainer = ($tempPageContent) ? $tempPageContent->find('div', 0) : null;
							if ($tempContainer) {
								$content = str_replace($aArray[$i]->outertext, $tempContainer->innertext, $content);
							}
						}
					}
				}
				
				$content = preg_replace('/(img|src)(\"|\'|\=\"|\=\')(.*)/i',"$1$2".$website."$3",$content);
				$content = str_replace('href="', 'href="'.$website.'', $content);
				$content = Gio_Core_String::stripTags($content, array('a'));
				
				$image = $container->find('img');
				$article['content'] = $content;
				$article['image_url'] = (count($image) > 0) ? $website . $image[0]->src : $article['image_url'];
			} 
		}
		return $article;
	}
	
	private function _zing($article)
	{		
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");

		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			
			$html = str_get_html($htmlString);
			$dateTime = trim($html->find('div.datetime', 0)->innertext);
			$dateTmp = explode(' ', $dateTime);
			$dateTime = $dateTmp[2] . ' ' . $dateTmp[3];
			$dateTime = date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $dateTime)));
			
			$pageContent = $html->find('div[id=content_document]', 0);
			$container = $pageContent;
			if ($container) {
				$title = $container->find('h1.pTitle', 0)->outertext;
				$content = str_replace($title, '', $container->innertext);
				$content = Gio_Core_String::stripTags($content, array('a'));
				
				$image = $container->find('img');
				$article['content'] = $content;
				$article['image_url'] = (count($image) > 0) ? $image[0]->src : $article['image_url'];
				$article['created_date'] = $dateTime;
			} 
		}
		return $article;
	}
	
	private function _megafun($article)
	{	
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");	
		$website = 'http://megafun.vn';
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			
			$html = str_get_html($htmlString);
			
			$date = $html->find('.date', 0);
			$date = preg_replace('/(.*)([0-9\:]{5})(.*)([0-9\/]{10})(.*)/i', '$2 $4', $date->plaintext);
			$date = date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $date)));
			
			$pageContent = $html->find('div[id=content]', 0);
			$container = $pageContent;
			if ($container) {
				$description = $container->find('span', 0);
				$content = $container->innertext;
				$content = Gio_Core_String::stripTags($content, array('a'));
				$content = preg_replace('/(img|src)(\"|\'|\=\"|\=\')(.*)/i',"$1$2".$website."$3",$content);
				
				$image = $container->find('img');
				$article['description'] = (isset($description->plaintext)) ? $description->plaintext : null;
				$article['content'] = $content;
				$article['image_url'] = (count($image) > 0) ? $website . $image[0]->src : $article['image_url'];
				$article['created_date'] = $date;
			} 
		}
		return $article;
	}
	
	private function _kenh14($article)
	{
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");		
		$website = 'http://kenh14.vn';
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}

			$html = str_get_html($htmlString);
			
			$pageContent = $html->find('div.postpadding', 0);
			$container = $pageContent;
					
			if ($container) {
				$desccription = $container->find('.sapo', 0)->outertext;
				$content = $container->find('div.content', 0)->innertext;
				$content = $description . $content;
				$author = $container->find('div.author', 0)->plaintext;
				$content .= '<p style="text-align: right;">' . $author . '</p>';
				
				$date = $container->find('div.meta', 0);
				$date = str_replace('/', '-', trim($date->plaintext));
				$date = date('Y-m-d H:i:s', strtotime($date));
				
				$content = Gio_Core_String::stripTags($content, array('script'), true);
				
				$image = $container->find('img');
				$article['content'] = $content;
				
				$article['image_url'] = (count($image) > 1) ? $image[1]->src : $article['image_url'];
				$article['created_date'] = $date;
			} 
		}
		return $article;
	}
	
	private function _ngoisao($article)
	{
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");
		$website = 'http://ngoisao.net';
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			$html = str_get_html($htmlString);
			
			$pageContent = $html->find('table[id=CContainer]', 0);
			$container = $pageContent->find('div', 0);
			
			if ($container) {
				$title = $container->find('.Title', 0)->outertext;
				$content = str_replace($title, '', $container->innertext);
				
				$image = $container->find('img');
				$content = str_replace($image[count($image) - 1]->outertext, '', $content);
				
				$aArray = $container->find('a');
				if (count($aArray) > 0) {
					for ($i = 0 ; $i < count($aArray); $i++) {
						$href = strtolower($aArray[$i]->href);
						if (strpos($href, 'page_') !== false) {
							$tempLink = $article['link_source'] . $aArray[$i]->href;
							$tempContent = @file_get_contents($tempLink);
							$tempHtml = str_get_html($tempContent);
							$tempPageContent = $tempHtml->find('table[id=CContainer]', 0);
							$tempContainer = ($tempPageContent) ? $tempPageContent->find('div', 0) : null;
							if ($tempContainer) {
								$content = str_replace($aArray[$i]->outertext, $tempContainer->innertext, $content);
							}
						}
					}
				}
				
				$content = preg_replace('/(img|src)(\"|\'|\=\"|\=\')(.*)/i',"$1$2".$article['link_source']."$3", $content);
				$content = Gio_Core_String::stripTags($content, array('a'));
				
				$article['content'] = $content;
				$article['image_url'] = (count($image) > 1) ? $article['link_source'] . $image[0]->src : $article['image_url'];
			} 
		}
		return $article;
	}
	
	private function _haitugio($article)
	{	
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");	
		$website = 'http://www.24h.com.vn';
		
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			
			$configs = Gio_Core_Config_Xml::getConfig();
			
			$html = str_get_html($htmlString);
			$video = false;
			
			$pageContent = $html->find('div.content-center', 0);
			if (!isset($pageContent->innertext) || null == $pageContent->innertext) {
				$pageContent = $html->find('div.content-center_video', 0);
				$video = true;
			}
			
			if (null == $pageContent) {
				return;
			}
			
			$container = $pageContent->find('.baiviet-container', 0);
			if (!isset($container->innertext) || null == $container->innertext) {
				$container = $pageContent->find('.baiviet-TopContent', 0);
				if (null == $container) {
					return;
				}
				$container = $container->find('div.video_container', 0);
				$video = true;
			}
			if ($container) {
				$content = $container->innertext;
				
				$tags = $container->find('div.baiviet-tags', 0);
				if (isset($tags->outertext)) {
					$content = str_replace($tags->outertext, '', $content);
				}
				
				$content = preg_replace('/(http:\/\/img)(.*)/i', rtrim($configs->web->url, '/') . '/flash.php?url=$1$2', $content);
				$content = str_replace('file=/upload/', 'file=' . $website . '/upload/', $content);
				$content = str_replace(',/upload/', ',' . $website . '/upload/', $content);
				
				$relates = $container->find('div.box-bai-lien-quan-ngang', 0);
				if (isset($relates->outertext)) {
					$content = str_replace($relates->outertext, '', $content);
				}
				$content = '<div><b>' . $article['description'] . '</b></div>' . $content;
				
				$htmlContent = str_get_html($content);
				$image = $htmlContent->find('img');
				if (count($image) > 0) {
					for ($i = 0; $i < count($image); $i++) {
						if (strpos($image[$i]->src, '../') !== false) {
							$newSrc = rtrim($configs->web->url, '/'). '/images.php?url=' . $website . '/' . str_replace('../', '', $image[$i]->src);
							$content = str_replace($image[$i]->src, $newSrc, $content);
						} else {
							$newSrc = rtrim($configs->web->url, '/') . '/images.php?url=' . $image[$i]->src;
							$content = str_replace($image[$i]->src, $newSrc, $content);
						}
					}
				}
				
				$htmlContent = str_get_html($content);
				$div = $htmlContent->find('div');
				if (isset($div[count($div) - 1]->outertext)) {
					$content = str_replace($div[count($div) - 1]->outertext, '', $content);
				}
				
				$tables = $htmlContent->find('table');
				for ($i = 0; $i < count($tables); $i++) {
					$content = str_replace($tables[$i]->outertext, '', $content);
				}
				
				$linksExternal = $htmlContent->find('.linkexternal');
				for ($i = 0; $i < count($linksExternal); $i++) {
					$content = str_replace($linksExternal[$i]->outertext, '', $content);
				} 
				
				$linksExternal = $htmlContent->find('.link_external');
				for ($i = 0; $i < count($linksExternal); $i++) {
					$content = str_replace($linksExternal[$i]->outertext, '', $content);
				}
				
				$image = $htmlContent->find('img');
				$article['content'] = $content;
				$article['article_video'] = (int)$video;
				$article['image_url'] = (count($image) > 1) ? $image[0]->src : $article['image_url'];
			} 
		}
		return $article;
	}
	
	private function _tuoitre($article)
	{		
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");
		$website = 'http://tuoitre.vn';
		
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			$html = str_get_html($htmlString);
			
			$pageContent = $html->find('div[id=divContent]', 0);
			$container = $pageContent;
				
			if ($container) {
				$content = $container->innertext;
				
				$pTitle = $container->find('.pTitle', 0);
				if (isset($pTitle->outertext)) {
					$content = str_replace($pTitle->outertext, '', $content);
				}
				
				$p = $container->find('p');
				for ($i = 1; $i <= 3; $i++) {
					if (isset($p[count($p) - $i]) && $p[count($p) - $i]->class != 'pAuthor'
							&& $p[count($p) - $i]->class != 'pHead' && $p[count($p) - $i]->class != 'pBody') 
					{
						$content = str_replace($p[count($p) - $i]->outertext, '', $content);
					}
				}
				
				$image = $container->find('img');
				$article['content'] = $content;
				$article['image_url'] = (count($image) > 0) ? $image[0]->src : $article['image_url'];
			} 
		}
		return $article;
	}
	
	private function _thanhnien($article)
	{		
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");
		$website = 'http://www.thanhnien.com.vn';
		
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			$html = str_get_html($htmlString);
			
			$dateContainer = $html->find('div.dateStart', 0);
			$dateTime = date('Y-m-d H:i:s');
			if ($dateContainer) {
				$dateTime = $dateContainer->plaintext;
				$dateTime = str_replace('/', '-', $dateTime);
				$dateTime = str_replace('&nbsp;', '', $dateTime);
				$dateTime = date('Y-m-d H:i:s', strtotime($dateTime));
			}
			
			$pageContent = $html->find('div.pageImgCont', 0);
			$container = $pageContent;
						
			if ($container) {
				$content = $container->innertext;
				$lastTable = $container->find('table');
				$content = str_replace($lastTable[count($lastTable) - 1]->outertext, '', $content);
				
				$image = $container->find('img');
				
				$content = preg_replace('/(img|src)(\"|\'|\=\"|\=\'|\=)(.*)/i',"$1$2".$website."$3",$content);
				
				$article['content'] = $content;
				
				$article['image_url'] = (count($image) > 4) ? $website . $image[0]->src : $article['image_url'];
				$article['created_date'] = $dateTime;
			} 
		}
		return $article;
	}
	
	private function _nguoilaodong($article)
	{		
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");
		$website = 'http://nld.com.vn';
		
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			
			$html = str_get_html($htmlString);
			
			$pageContent = $html->find('div[id=ctl00_ContentPlaceHolder1_divDetailNews]', 0);
			$container = $pageContent->find('div.tinchitiet', 0);
				
			if ($container) {
				$content = $container->innertext;
				
				/**
				 * Row 0
				 */
				$row0 = $container->find('div.row', 0);
				if (isset($row0->outertext)) {
					$content = str_replace($row0->outertext, '', $content);
				}
				/**
				 * Row 1
				 */
				$row1 = $container->find('div.row', 1);
				if (isset($row1->outertext)) {
					$content = str_replace($row1->outertext, '', $content);
				}
				/**
				 * SubTitle
				 */
				$subTitle = $container->find('div[id=ctl00_ContentPlaceHolder1_ctl00_divSubTitle]', 0);
				if (isset($subTitle->outertext)) {
					$content = str_replace($subTitle->outertext, '', $content);
				}
				/**
				 * Title
				 */
				$title = $container->find('div.title', 0);
				if (isset($title->outertext)) {
					$content = str_replace($title->outertext, '', $content);
				}
				/**
				 * Social Share
				 */
				$lastDiv = $container->find('div');
				if (isset($lastDiv[count($lastDiv) - 1]->outertext)) {
					$content = str_replace($lastDiv[count($lastDiv) - 1]->outertext, '', $content);
				}
				
				$detailsContainer = $container->find('div.detail', 0);
				$image = $detailsContainer->find('img');
				
				$article['content'] = $content;
				$article['image_url'] = (count($image) > 0) ? $image[0]->src : $article['image_url'];
			} 
		}
		return $article;
	}
	
	private function _haisao($article)
	{	
		ini_set("max_execution_time", 100);	
		ini_set("memory_limit", "256M");
		$website = 'http://2sao.vietnamnet.vn';
		
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			
			$html = str_get_html($htmlString);
			
			$pageContent = $html->find('div[id=container]', 0);
			if (!isset($pageContent->innertext)) {
				$pageContent = $html->find('div.bodyConent', 0);
			}
			$container = $pageContent;
			
			if ($container) {
				$content = $container->find('div.detail_content', 0);
				$description = $container->find('.detail_sapo', 0);
				
				$voteContainer = $content->find('div.vote_default', 0);
				$content = $content->outertext;
				if (isset($voteContainer->outertext)) {
					$content = str_replace($voteContainer->outertext, '', $content);
				}
				
				$content = '<div class="detail_sapo">' . $description->plaintext . '</div>' . $content;
				$content = Gio_Core_String::stripTags($content, array('a'));
				
				$content = str_replace('src="/scripts/swfobject.packed.js"', 'src="' . $website . '/scripts/swfobject.packed.js"', $content);
				$content = str_replace('SWFObject("', 'SWFObject("' . $website, $content);
				
				$htmlContent = str_get_html($content);
				$image = $htmlContent->find('img');
				
				$article['content'] = $content;
				$article['image_url'] = (count($image) > 0) ? $image[0]->src : $article['image_url'];
			} 
		}
		return $article;
	}
	
	private function _autopro($article)
	{	
		ini_set("max_execution_time", 100);	
		ini_set("memory_limit", "256M");
		$website = 'http://autopro.com.vn';
		
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			
			$html = str_get_html($htmlString);
			
			$pageContent = $html->find('.tinchitiet', 0);
			$container = $pageContent;
			
			if ($container) {
				$content = $container->innertext;
				
				$content = '<div class="detail_sapo">' . $article['description'] . '</div><br /><br />' . $content;
				$content = Gio_Core_String::stripTags($content, array('a'));
				
				$authorContainer = $html->find('.editorCss', 0);
				if (isset($authorContainer->plaintext)) {
					$content .= '<p align="right" class="Normal"><b>' . $authorContainer->plaintext . '</b></p>';
				}
				
				$createdDate = $html->find('.datepost', 0);
				$temp = explode(',', $createdDate->innertext);
				$stringDate = trim($temp[1]) . ' ' . trim($temp[2]);
				$stringDate = str_replace('/', '-', $stringDate);
				$stringDate = substr($stringDate, 0, -7);
				$createdDate = date('Y-m-d H:i:s', strtotime($stringDate));
				
				$htmlContent = str_get_html($content);
				$image = $htmlContent->find('img');
				
				$article['article_photo'] = 1;
				$article['created_date'] = $createdDate;
				$article['content'] = $content;
				$article['image_url'] = (count($image) > 0) ? $image[0]->src : $article['image_url'];
			} 
		}
		return $article;
	}
	
	private function _bongda($article)
	{		
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");
		$website = 'http://www.bongda.com.vn';
		
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			$html = str_get_html($htmlString);
			
			$pageContent = $html->find('div[id=ctl00_BD_art_pnlNewsContainer]', 0);
			$container = $pageContent;
			
			if ($container) {
				$descriptionContainer = $container->find('font', 0);
				$description = trim($descriptionContainer->plaintext);
				if (null == $description || $description == '') {
					$descriptionContainer = $container->find('font', 1);
					$description = $descriptionContainer->plaintext;
				}
				
				$content = $container->innertext;
				$as = $container->find('a.read_more');
				for($i = 0; $i < count($as); $i++) {
					$content = str_replace($as[$i]->outertext, '', $content);
				}
				$content = str_replace('&gt;', '', $content);
														
				$htmlContent = str_get_html($content);
				$fonts = $htmlContent->find('font');
				for($i = 0; $i < count($fonts); $i++) {
					$temp = strip_tags($fonts[$i]->innertext);
					$temp = trim($temp);
					if (null == $temp) {
						$content = str_replace($fonts[$i]->outertext, '', $content);
					}
				}
				
				$image = $htmlContent->find('img');
				$content = Gio_Core_String::stripTags($content, array('a'));
				$article['content'] = $content;
				$article['description'] = $description;
				$article['image_url'] = (count($image) > 0) ? $image[0]->src : $article['image_url'];
			}
		}
		return $article;
	}
	
	private function _tintuconline($article)
	{		
		ini_set("max_execution_time", 100);
		ini_set("memory_limit", "256M");
		$website = 'http://www.tintuconline.com.vn';
		
		if (isset($article['link_source']) && $article['link_source']) {
			$html = null;
			if (($htmlString = @file_get_contents($article['link_source'])) === false) {
				return $article;	
			}
			$html = str_get_html($htmlString);
			
			$pageContent = $html->find('div[id=newsContent]', 0);
			$container = $pageContent;
					
			if ($container) {
				$content = $container->innertext;
				$tablesRelate = $container->find('.newsRelate');
				for ($i = 0; $i < count($tablesRelate); $i++) {
					$content = str_replace($tablesRelate[$i]->outertext, '', $content);
				}
				
				$images = $container->find('img');
				for ($i = 0; $i < count($images); $i++) {
					if (strpos($images[$i]->src, 'http://') === false) {
						$content = str_replace($images[$i]->src, $website . $images[$i]->src, $content);
					}
				}

				$photoAlbum = $html->find('div[id=photoAlbum]', 0);
				if (isset($photoAlbum->innertext) && $photoAlbum->innertext != null) {
					$imagesAlbum = $photoAlbum->find('.image');
					$titlesAlbum = $photoAlbum->find('.title');
					for ($i = 0; $i < count($imagesAlbum); $i++) {
						$imageAlbum = $imagesAlbum[$i]->find('img', 0); 
						$src = $imageAlbum->src;
						if (strpos($imageAlbum->src, 'http://') === false) {
							$src = $website . $imageAlbum->src;
						}
						$content .= '<p><img src="' . $src . '" alt="" /></p>';
						$title = isset($titlesAlbum[$i]) ? $titlesAlbum[$i]->plaintext : null;
						$content .= '<p>' . $title . '</p>';
					}
				}
				
				$htmlContent = str_get_html($content);
				$images = $htmlContent->find('img');
				$content = Gio_Core_String::stripTags($content, array('a'));
				$article['content'] = $content;
				$article['image_url'] = (count($images) > 0) ? $images[0]->src : $article['image_url'];
			}
		}
		return $article;
	}
}